#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>
#include <QLabel>
#include <QMutex>
#include <QQueue>
#include <QDir>
#include <QFile>
#include <QtConcurrent/QtConcurrentRun>
#include <QFutureWatcher>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class ReadThread;

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;

private slots:
    void tryOpenFIFO();
    void updateConnectionStatus(bool connected);
    void onNewData(QString line);
    void processData();
    void parseDataLine(const QString &line);
    void setDishRecommendation(const QString &dish);
    void setFoodState(const QString &state);

private:
    Ui::Widget *ui;
    QDir dir;
    uint ImageCount;
    uint currentImageIndex;
    QTimer *fifoRetryTimer;
    QTimer *processTimer;
    QTimer *updateTimer;
    ReadThread *m_readThread;
    QLabel *statusLabel;
    QQueue<QString> dataQueue;
    QFile fifoFile;
    QMutex queueMutex;

    void loadImage();
    void handleImageClick();
    void loadAndDisplayImage(uint index);
};

#endif // WIDGET_H
